package com.chuangke.dev.controller;

import com.chuangke.common.http.HttpResult;
import com.chuangke.common.utils.ConvertUtils;
import com.chuangke.common.utils.ZipUtils;
import com.chuangke.dev.model.SysFormModel;
import com.chuangke.dev.service.SysFormModelService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/model")
public class SysFormModelController {
	@Autowired
    private SysFormModelService sysFormModelService;
//	@Autowired
//	private SysModelTableService genTableService;
	
    @PreAuthorize("hasAuthority('login:action')")
    @GetMapping("/list")
    public HttpResult list(@RequestParam String name) {
        List<SysFormModel> list = sysFormModelService.selectSysFormModelList(name);
        return HttpResult.ok(list);
    }

    @PreAuthorize("hasAuthority('login:action')")
    @PostMapping(value = "/save")
    public HttpResult save(@RequestBody SysFormModel sysFormModel) {
    	sysFormModelService.saveOrUpdateSysFormModel(sysFormModel);
        return HttpResult.ok();
    }
    @PreAuthorize("hasAuthority('login:action')")
	@DeleteMapping("/{ids}")
    public HttpResult remove(@PathVariable List<String> ids) {
    	sysFormModelService.deleteSysFormModelByIds(ids);
        return HttpResult.ok();
    }
    
    /**
	 * 批量生成代码
	 */
	@GetMapping("/batchGenCode")
	public void batchGenCode(HttpServletResponse response, String modelId) {
		String[] modelIds = ConvertUtils.toStrArray(modelId);
		Map<String, byte[]> fileMap = sysFormModelService.generatorFormModelCode(modelIds);
		ZipUtils.downloadFileZip(response, "chuangke", fileMap);
	}
}
